package linknode;

public class LinkNodeTailToHead {

    // 反转
    public LinkNode reverseList(LinkNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        LinkNode pre = null;
        LinkNode cur = head;
        while (cur != null) {
            cur.next = pre;
            pre = cur;
            cur = cur.next;
        }
        return pre;
    }


    // 用递归实现反转
    public static LinkNode reverse2(LinkNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        LinkNode newHead = reverse2(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    public static void main(String[] args) {
        int[] list = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        LinkNode head = LinkNodeTraverse.buildLinkNode(list);
        System.out.println(reverse2(head).data);
    }

}
